﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace FunasrSTT.Translate.Converters
{
    public class EnumToBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null) return false;
            string checkValue = value.ToString();
            string targetValue = parameter.ToString();
            // 处理带命名空间的枚举值
            if (targetValue.Contains('.'))
            {
                targetValue = targetValue.Split('.').Last();
            }
            return checkValue.Equals(targetValue, StringComparison.InvariantCultureIgnoreCase);
            //return value == null ? false : value.Equals(parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null) return Binding.DoNothing;
            if ((bool)value)
            {
                try
                {
                    string enumValue = parameter.ToString();
                    // 处理带命名空间的枚举值
                    if (enumValue.Contains('.'))
                    {
                        enumValue = enumValue.Split('.').Last();
                    }

                    return Enum.Parse(targetType, enumValue);
                }
                catch
                {
                    return Binding.DoNothing;
                }
            }

            return Binding.DoNothing;
            //return value != null && value.Equals(true) ? parameter : Binding.DoNothing;
        }
    }
}
